package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.UserAliases;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 * Created by xiangnan on 15-12-27.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class UserAliaseDao extends BaseDao<UserAliases>{
    public UserAliases getByTypeAndNumber(int type, String alipayNumber) {

        Query query = em.createNamedQuery("UserAliases.getByTypeAndNumber");
        query.setParameter("type",type).setParameter("alipayNumber",alipayNumber);
        try {
            return (UserAliases) query.getSingleResult();
        } catch (NonUniqueResultException e){
            return (UserAliases) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }
    public UserAliases getByUserIdAndType(long userId, int type) {
        Query query = em.createNamedQuery("UserAliases.getByUserIdAndType");
        query.setParameter("userId",userId).setParameter("type",type);
        try {
            return (UserAliases) query.getSingleResult();
        } catch (NonUniqueResultException e){
            return (UserAliases) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }
}
